package ${package.Controller};

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import ${package.Entity}.vo.${table.entityName}Vo;
import com.mp.demo.utils.DozerUtils;
import org.dozer.Mapper;
import org.springframework.web.bind.annotation.*;

<#if restControllerStyle>
import org.springframework.web.bind.annotation.RestController;
<#else>
import org.springframework.stereotype.Controller;
</#if>
import org.springframework.beans.factory.annotation.Autowired;
import io.swagger.annotations.Api;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import java.util.*;
import com.mp.demo.common.Result;
import  ${package.Service}.${table.serviceName};
import  ${package.Entity}.${table.entityName};
<#if superControllerClassPackage??>
import ${superControllerClassPackage};
</#if>
import org.springframework.web.servlet.ModelAndView;
import org.springframework.security.access.prepost.PreAuthorize;

/**
* <p>
    * ${table.comment!} 前端控制器
    * </p>
*
* @author ${author}
* @since ${date}
*/
<#if restControllerStyle>
@RestController
<#else>
@Controller
</#if>
@Slf4j
@Api(value = "${table.entityName}-Controller")
@RequestMapping("<#if package.ModuleName??>/${package.ModuleName}</#if>/<#if controllerMappingHyphenStyle??>${controllerMappingHyphen}<#else>${table.entityPath}</#if>")
<#if kotlin>
class ${table.controllerName}<#if superControllerClass??> : ${superControllerClass}()</#if>
<#else>
    <#if superControllerClass??>
    public class ${table.controllerName} extends ${superControllerClass} {
    <#else>
    public class ${table.controllerName} {
    </#if>

    @Autowired
    private Mapper dozerMapper;
    @Autowired
    private ${table.serviceName} ${table.entityName?uncap_first}Service;

    @RequestMapping(value = "/list-view",method = RequestMethod.GET)
    //@PreAuthorize("hasAuthority('${table.entityName?uncap_first}:list')")
    public String list(){
        return "/${table.entityName?uncap_first}/list";
    }

    @ApiOperation(value = "获取${table.entityName?uncap_first}-list-all")
    @GetMapping("/list-all")
    @ResponseBody
    //@PreAuthorize("hasAuthority('${table.entityName?uncap_first}:query')")
    public Result listAll(@RequestParam(required = false) String search) throws Exception {
        QueryWrapper<${table.entityName}> wrapper = new QueryWrapper<>();
        //todo 需自行设置查询字段
        Collection<${table.entityName}> ${table.entityName?uncap_first}List= ${table.entityName?uncap_first}Service.list(wrapper);
        List<${table.entityName}Vo> ${table.entityName?uncap_first}Vos = DozerUtils.mapList(dozerMapper, ${table.entityName?uncap_first}List, ${table.entityName}Vo.class);
        return Result.success(${table.entityName?uncap_first}Vos);
    }

    @ApiOperation(value = "获取${table.entityName?uncap_first}-list-page")
    @GetMapping("/list-page")
    @ResponseBody
    //@PreAuthorize("hasAuthority('${table.entityName?uncap_first}:query')")
    public Result listPage(@RequestParam(required = false,defaultValue = "0") Integer page
                            ,@RequestParam(required = false,defaultValue = "20") Integer limit
                            ,@RequestParam(required = false) String search) throws Exception {
        IPage<${table.entityName}> iPage = new Page<>(page, limit);
        QueryWrapper<${table.entityName}> wrapper = new QueryWrapper<>();
        //todo 需自行设置查询字段
        IPage<${table.entityName}> resultPage =  ${table.entityName?uncap_first}Service.page(iPage, wrapper);
        return Result.success(resultPage);
    }

    @ApiOperation(value = "查询${table.entityName?uncap_first}-id")
    @GetMapping("/query/{id}")
    @ResponseBody
    //@PreAuthorize("hasAuthority('${table.entityName?uncap_first}:query')")
    public Result query(@PathVariable <#list table.fields as field><#if field.keyFlag == true>${field.columnType?lower_case?cap_first}</#if></#list> id) throws Exception {
        QueryWrapper<${table.entityName}> wrapper = new QueryWrapper<>();
        wrapper.eq("id",id);
        ${table.entityName} ${table.entityName?uncap_first} = ${table.entityName?uncap_first}Service.getOne(wrapper);
        if (null != ${table.entityName?uncap_first})
            return  Result.success(dozerMapper.map(${table.entityName?uncap_first},${table.entityName}Vo.class));
        return Result.success(${table.entityName?uncap_first});
    }

    @RequestMapping(value = "/update-view/{id}",method = RequestMethod.GET)
    //@PreAuthorize("hasAuthority('${table.entityName?uncap_first}:update')")
    public ModelAndView update(ModelAndView mv,@PathVariable Integer id){
        ${table.entityName} ${table.entityName?uncap_first} = ${table.entityName?uncap_first}Service.getById(id);
        mv.addObject("${table.entityName?uncap_first}",${table.entityName?uncap_first});
        mv.setViewName("/${table.entityName?uncap_first}/list");
        return mv;
    }

    @ApiOperation(value = "修改${table.entityName?uncap_first}")
    @PostMapping("/update")
    @ResponseBody
    //@PreAuthorize("hasAuthority('${table.entityName?uncap_first}:update')")
    public Result update(@RequestBody  ${table.entityName} ${table.entityName?uncap_first}) throws Exception {
        Boolean flag = ${table.entityName?uncap_first}Service.updateById(${table.entityName?uncap_first});
        return Result.defaultMsg(flag);
    }

    @ApiOperation(value = "删除${table.entityName?uncap_first}")
    @DeleteMapping("/del/{id}")
    @ResponseBody
    //@PreAuthorize("hasAuthority('${table.entityName?uncap_first}:delete')")
    public Result delete(@PathVariable <#list table.fields as field><#if field.keyFlag == true>${field.columnType?lower_case?cap_first}</#if></#list> id) throws Exception {
        Boolean flag = ${table.entityName?uncap_first}Service.removeById(id);
        return Result.defaultMsg(flag);
    }

    @RequestMapping(value = "/add-view",method = RequestMethod.GET)
    //@PreAuthorize("hasAuthority('${table.entityName?uncap_first}:add')")
    public String add(){
        return "/${table.entityName?uncap_first}/add";
    }

    @ApiOperation(value = "添加${table.entityName?uncap_first}")
    @PutMapping("/add")
    @ResponseBody
    //@PreAuthorize("hasAuthority('${table.entityName?uncap_first}:add')")
    public Result add(@RequestBody  ${table.entityName} ${table.entityName?uncap_first}) throws Exception {
        Boolean flag = ${table.entityName?uncap_first}Service.save(${table.entityName?uncap_first});
        return Result.defaultMsg(flag);
    }
}
</#if>
